/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mybatis.wizard;

import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.StringWriter;
import java.util.ArrayList;
import javax.swing.JPanel;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultCellEditor;
import javax.swing.ListSelectionModel;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.mybatis.model.BatisColumnTypeClass;
import org.mybatis.model.CheckItem;
import org.mybatis.model.CheckListRenderer;
import org.mybatis.model.ComboBoxModelJDBCConnection;
import org.mybatis.model.ComboBoxModelTable;
import org.mybatis.model.DBConnection;
import org.mybatis.model.DataBaseMetaDataView;
import org.mybatis.model.DataBaseTable;
import org.mybatis.model.DataBaseTableColumn;
import org.mybatis.model.ListAtributosMapper;
import org.mybatis.model.ListModelCheck;
import org.mybatis.model.ListModelColumn;
import org.mybatis.model.TableModelTypeColumn;
import org.mybatis.xml.Atributo;
import org.mybatis.xml.Mapper;
import org.openide.util.Exceptions;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public final class MyBatisMapperVisualPanel2 extends JPanel {

    static final String XMLSTRING = "XMLSTRING";
    private java.sql.Connection cn;
    private DataBaseMetaDataView DBMetaData;
    private ListModelColumn listModelColumnAvailable;
    private ListModelColumn listModelColumnSelected;
    TableModelTypeColumn tableModelType;
    javax.swing.JComboBox<String> objectType = new javax.swing.JComboBox<String>();
    private Hashtable<String, String> mapType = new Hashtable<String, String>();
    private ListModelCheck listCheckSelect;
    private ListModelCheck listCheckUpdate;
    private ListModelCheck listCheckInsert;
    /**
     * Creates new form MyBatisMapperVisualPanel1
     */
    private String xml;

    public String getXmlString() {
        return this.xml;
    }

    public MyBatisMapperVisualPanel2() {
        initComponents();
        this.jComboBox1.setModel(new ComboBoxModelJDBCConnection());
        //this.jPanel2.setVisible(false);
        mapType.put("serial", Integer.class.toString());
        mapType.put("int4", Integer.class.toString());
        mapType.put("varchar", String.class.toString());
        mapType.put("bool", Boolean.class.toString());
        mapType.put("double", Double.class.toString());
        mapType.put("timestamp", java.sql.Timestamp.class.toString());

        fillTypeComboBox();
        fillPropertiesListInsert();
        fillPropertiesListUpdate();
        fillPropertiesListSelect();

    }

    private void fillPropertiesListInsert() {

        //CheckItem[] elements = new CheckItem[10];
        List<CheckItem> listItem = new ArrayList<CheckItem>();
        ListAtributosMapper atributosMapper = new ListAtributosMapper();
        for (int i = 0; atributosMapper.getAtributosInsert().size() > i; i++) {
            listItem.add(new CheckItem(atributosMapper.getAtributosInsert().get(i), false));
        }

        listCheckInsert = new ListModelCheck(listItem);
        jList6.setModel(listCheckInsert); //= new javax.swing.JList<CheckItem>(elements);
        //jList6.setModel(listModelColumnSelected);
        jList6.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //jList6.setBorder(new EtchedBorder());
        this.jList6.setCellRenderer(new CheckListRenderer());
        this.jList6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int index = jList6.locationToIndex(e.getPoint());
                CheckItem item = (CheckItem) jList6.getModel().getElementAt(index);

                item.setSelected(!item.isSelected());
                Rectangle rect = jList6.getCellBounds(index, index);
                jList6.repaint(rect);
                if (item.isSelected()) {
                    String valor = javax.swing.JOptionPane.showInputDialog(jList6, "Introducir un valor");
                    if (valor != null) {
                        item.getAttr().setValor(valor);
                    } else {
                        item.setSelected(false);
                    }
                }
            }
        });

    }

    private void fillPropertiesListUpdate() {

        //CheckItem[] elements = new CheckItem[10];
        List<CheckItem> listItem = new ArrayList<CheckItem>();
        ListAtributosMapper atributosMapper = new ListAtributosMapper();
        for (int i = 0; atributosMapper.getAtributosUpdate().size() > i; i++) {
            listItem.add(new CheckItem(atributosMapper.getAtributosUpdate().get(i), false));
        }

        listCheckUpdate = new ListModelCheck(listItem);
        jList7.setModel(listCheckUpdate); //= new javax.swing.JList<CheckItem>(elements);
        //jList6.setModel(listModelColumnSelected);
        jList7.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //jList6.setBorder(new EtchedBorder());
        this.jList7.setCellRenderer(new CheckListRenderer());
        this.jList7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int index = jList7.locationToIndex(e.getPoint());
                CheckItem item = (CheckItem) jList7.getModel().getElementAt(index);

                item.setSelected(!item.isSelected());
                Rectangle rect = jList7.getCellBounds(index, index);
                jList7.repaint(rect);
                if (item.isSelected()) {
                    String valor = javax.swing.JOptionPane.showInputDialog(jList7, "Introducir un valor");
                    if (valor != null) {
                        item.getAttr().setValor(valor);
                    } else {
                        item.setSelected(false);
                    }
                }
            }
        });

    }

    private void fillPropertiesListSelect() {

        //CheckItem[] elements = new CheckItem[10];
        List<CheckItem> listItem = new ArrayList<CheckItem>();
        ListAtributosMapper atributosMapper = new ListAtributosMapper();
        for (int i = 0; atributosMapper.getAtributosSelect().size() > i; i++) {
            listItem.add(new CheckItem(atributosMapper.getAtributosSelect().get(i), false));
        }

        listCheckSelect = new ListModelCheck(listItem);
        jList8.setModel(listCheckSelect); //= new javax.swing.JList<CheckItem>(elements);
        //jList6.setModel(listModelColumnSelected);
        jList8.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //jList6.setBorder(new EtchedBorder());
        this.jList8.setCellRenderer(new CheckListRenderer());
        this.jList8.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int index = jList8.locationToIndex(e.getPoint());
                CheckItem item = (CheckItem) jList8.getModel().getElementAt(index);

                item.setSelected(!item.isSelected());
                Rectangle rect = jList8.getCellBounds(index, index);
                jList8.repaint(rect);
                if (item.isSelected()) {
                    String valor = javax.swing.JOptionPane.showInputDialog(jList8, "Introducir un valor");
                    if (valor != null) {
                        item.getAttr().setValor(valor);
                    } else {
                        item.setSelected(false);
                    }
                }
            }
        });

    }

    private void fillTypeComboBox() {

        objectType.addItem(String.class.toString());
        objectType.addItem(Integer.class.toString());
        objectType.addItem(Double.class.toString());
        objectType.addItem(Boolean.class.toString());
        objectType.addItem(java.sql.Timestamp.class.toString());
        objectType.addItem(Float.class.toString());
        objectType.addItem(Byte.class.toString());


    }

    @Override
    public String getName() {
        return "Configuración DB Mapper";
    }

    private Object getTypeColumn(String typeCol) {
        return this.mapType.get(typeCol);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane8 = new javax.swing.JScrollPane();
        jList6 = new javax.swing.JList();
        jScrollPane9 = new javax.swing.JScrollPane();
        jList7 = new javax.swing.JList();
        jScrollPane10 = new javax.swing.JScrollPane();
        jList8 = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jScrollPane1.setViewportView(jList2);

        org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jButton2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jButton3.text")); // NOI18N

        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jLabel3.text")); // NOI18N

        jScrollPane2.setViewportView(jList1);

        org.openide.awt.Mnemonics.setLocalizedText(jButton4, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jButton4.text")); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton5, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jButton5.text")); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton6, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jButton6.text")); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton7, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jButton7.text")); // NOI18N
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox1, 0, 422, Short.MAX_VALUE)
                            .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButton6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton5, javax.swing.GroupLayout.Alignment.TRAILING)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton7))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                                    .addComponent(jScrollPane1)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(137, 137, 137)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Column name", "TYPE", "Alias class", "Primary key", "UseGeneratedKeys"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60))
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jCheckBox1, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jCheckBox1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jCheckBox2, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jCheckBox2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jCheckBox3, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jCheckBox3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jCheckBox4, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jCheckBox4.text")); // NOI18N
        jCheckBox4.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBox4ItemStateChanged(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane4.setViewportView(jTextArea1);

        jScrollPane8.setViewportView(jList6);

        jScrollPane9.setViewportView(jList7);

        jScrollPane10.setViewportView(jList8);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jLabel4.text")); // NOI18N

        jTextField1.setText(org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jTextField1.text")); // NOI18N
        jTextField1.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jLabel5.text")); // NOI18N

        jTextField2.setText(org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jTextField2.text")); // NOI18N
        jTextField2.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jCheckBox1)
                                .addGap(119, 119, 119)
                                .addComponent(jCheckBox2)
                                .addGap(116, 116, 116)
                                .addComponent(jCheckBox3))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jCheckBox4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 88, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBox1)
                            .addComponent(jCheckBox2)
                            .addComponent(jCheckBox3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox4)
                    .addComponent(jLabel4)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(MyBatisMapperVisualPanel2.class, "MyBatisMapperVisualPanel2.jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(48, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox2ItemStateChanged

        // this.jList1.setModel(null);
        this.DBMetaData.muestraColumnas("", ((DataBaseTable) this.jComboBox2.getModel().getSelectedItem()).getNomeTable());
        this.listModelColumnAvailable = new ListModelColumn(this.DBMetaData.getListColumn());
        this.jList1.setModel(this.listModelColumnAvailable);
        this.listModelColumnSelected = new ListModelColumn(new java.util.ArrayList<DataBaseTableColumn>());
        this.jList2.setModel(listModelColumnSelected);

        this.tableModelType = new TableModelTypeColumn(new Vector());
        this.jTable1.setModel(tableModelType);
        this.jTable1.getColumn("TYPE").setCellEditor(new DefaultCellEditor(objectType));
    }//GEN-LAST:event_jComboBox2ItemStateChanged

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged

        if (evt.getSource() == jComboBox1) {
            this.DBMetaData = new DataBaseMetaDataView((DBConnection) this.jComboBox1.getModel().getSelectedItem());

            this.jComboBox2.setModel(new ComboBoxModelTable(DBMetaData.getListTable()));
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        int[] indices = this.jList1.getSelectedIndices();

        for (int x = 0; indices.length > x; x++) {
            DataBaseTableColumn rowTable = (DataBaseTableColumn) this.jList1.getModel().getElementAt(indices[x]);
            this.listModelColumnSelected.addItem(rowTable);
            this.tableModelType.setRow(new BatisColumnTypeClass(rowTable.getNomeColumn(), getTypeColumn(rowTable.getTypeColumn()), rowTable.getNomeColumn(), false));

        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

        for (int x = 0; this.jList1.getModel().getSize() > x; x++) {
            this.listModelColumnSelected.addItem((DataBaseTableColumn) this.jList1.getModel().getElementAt(x));
        }

    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed

        this.listModelColumnSelected.deleteItem((DataBaseTableColumn) this.jList2.getModel().getElementAt(this.jList2.getSelectedIndex()));
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed

        this.tableModelType = new TableModelTypeColumn(new Vector());
        this.listModelColumnSelected.deleteAll();

    }//GEN-LAST:event_jButton7ActionPerformed

    private void jCheckBox4ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox4ItemStateChanged

        if (jCheckBox4.isEnabled()) {
            this.jTextField1.setEnabled(!this.jTextField1.isEnabled());
            this.jTextField2.setEnabled(!this.jTextField2.isEnabled());
            GenXML();
        }

    }//GEN-LAST:event_jCheckBox4ItemStateChanged
    public String GenXML() {


        Mapper mp = configurarMapper();

        Element e = null;
        Node n = null;
// Document (Xerces implementation only).
        Document xmldoc = new DocumentImpl();
// Root element.


        Element root = xmldoc.createElement("mapper");

        root.setAttributeNS(null, "namespace", mp.getRoot().substring(0, 1).toUpperCase() + mp.getRoot().substring(1, mp.getRoot().length()));

        e = xmldoc.createElementNS(null, "insert");
        for (int x = 0; mp.getInsertAtributos().size() > x; x++) {
            if (mp.getInsertAtributos().get(x).isSelected()) {
                e.setAttributeNS(null, mp.getInsertAtributos().get(x).getAttr().getNombre(), mp.getInsertAtributos().get(x).getAttr().getValor());
            }
        }
        n = xmldoc.createTextNode(mp.getInsertValor());
        e.appendChild(n);
        root.appendChild(e);

        e = xmldoc.createElementNS(null, "update");
        for (int x = 0; mp.getUpdateAtributos().size() > x; x++) {
            if (mp.getUpdateAtributos().get(x).isSelected()) {
                e.setAttributeNS(null, mp.getUpdateAtributos().get(x).getAttr().getNombre(), mp.getUpdateAtributos().get(x).getAttr().getValor());
            }
        }
        n = xmldoc.createTextNode(mp.getUpdateValor());
        e.appendChild(n);
        root.appendChild(e);



        e = xmldoc.createElementNS(null, "select");
        for (int x = 0; mp.getSelectAtributos().size() > x; x++) {
            if (mp.getSelectAtributos().get(x).isSelected()) {
                e.setAttributeNS(null, mp.getSelectAtributos().get(x).getAttr().getNombre(), mp.getSelectAtributos().get(x).getAttr().getValor());
            }
        }
        n = xmldoc.createTextNode(mp.getSelectValor());
        e.appendChild(n);
        root.appendChild(e);

        e = xmldoc.createElementNS(null, "resultMap");
        for (int x = 0; mp.getResultMap().size() > x; x++) {
            e.setAttributeNS(null, mp.getResultMap().get(x).getNombre(), mp.getResultMap().get(x).getValor());
        }

        Element er = null;

        for (int x = 0; mp.getListColumnProperty().size() > x; x++) {

            if (mp.getListColumnProperty().get(x).getPRIMARY_KEY()) {
                er = xmldoc.createElementNS(null, "id");
                er.setAttributeNS(null, "column", mp.getListColumnProperty().get(x).getCOLUMN_NAME());
                er.setAttributeNS(null, "property", mp.getListColumnProperty().get(x).getCOLUMN_ALIAS());
                e.appendChild(er);
            } else {
                er = xmldoc.createElementNS(null, "result");
                er.setAttributeNS(null, "column", mp.getListColumnProperty().get(x).getCOLUMN_NAME());
                er.setAttributeNS(null, "property", mp.getListColumnProperty().get(x).getCOLUMN_ALIAS());
                e.appendChild(er);
            }
        }

        root.appendChild(e);
        xmldoc.appendChild(root);

        Source source = new DOMSource(xmldoc);

        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        try {
            trans = transfac.newTransformer();

            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            try {
                //Source source = new Source(document);
                trans.transform(source, result);
            } catch (TransformerException ex) {
                Exceptions.printStackTrace(ex);
            }

            String xmlString = sw.toString();
            this.jTextArea1.setText(xmlString);
            this.xml = xmlString;
            return xmlString;
        } catch (TransformerConfigurationException ex) {
            Exceptions.printStackTrace(ex);
            return null;
        }
    }

    private Mapper configurarMapper() {
        Mapper mp = new Mapper();
        mp.setRoot(((DataBaseTable) this.jComboBox2.getModel().getSelectedItem()).getNomeTable());
        String database = ((DataBaseTable) this.jComboBox2.getModel().getSelectedItem()).getDataBase();
        //database=database.substring(database.indexOf("/",3));
        //((DBConnection) this.jComboBox1.getModel().getSelectedItem()).getDataBaseURL().indexOf("/");
        mp.setBaseData(database);
        List<Atributo> rootAtributos = new ArrayList();
        rootAtributos.add(new Atributo("namespace", mp.getRoot()));


        mp.setRootAtributos(rootAtributos);

        mp.setNodoInsert("insert");
        mp.setInsertAtributos(this.listCheckInsert.getAllElements());

        mp.setNodoUpdate("update");
        mp.setUpdateAtributos(this.listCheckUpdate.getAllElements());



        mp.setNodoSelect("Select");
        mp.setSelectAtributos(this.listCheckSelect.getAllElements());
        // mp.setSelectValor("select * from user");

        mp.setNodoResultado("resultMap");

        List<Atributo> listAtributoResultado = new ArrayList<Atributo>();
        listAtributoResultado.add(new Atributo("type", mp.getRoot().substring(0, 1).toUpperCase() + mp.getRoot().substring(1, mp.getRoot().length())));
        listAtributoResultado.add(new Atributo("id", "resultado"));
        mp.setResultMap(listAtributoResultado);

        mp.setListColumnProperty(this.tableModelType.getRowAll());
        return mp;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList6;
    private javax.swing.JList jList7;
    private javax.swing.JList jList8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane10;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
